/*
 * Josh Lindoo
 * Login ID : lind6441
 * CS-102, Winter 2013
 * Programming Assignment 4
 * Database class: Storage for movies/showings, and methods for storing/using
 * 					those objects
 */

import java.util.Scanner;

public class Database {
	
	//Tree of movies
	private MovieTree movieTree = new MovieTree();
	
	//Constructor
	public Database() {	}
	
	
	/*
	 * Method: searchMovies
	 * Purpose: allow user to search for a specific movie title
	 * Parameters:
	 * 		String title:	title of movie to search for
	 * Returns: void
	 */
	
	public void searchMovies(String title) {
		if(!movieTree.titleLookup(title))
			System.out.println("No movie found..");
	}
	
	
	/*
	 * Method: storeMovies
	 * Purpose: parse the movie data from input file and save in db
	 * Parameters:
	 * 		Scanner fileScanner:	scanner of movie input file
	 * Returns: void
	 */
	
	public void storeMovies(Scanner fileScanner) {
		while(fileScanner.hasNextLine()) {	//while there are more lines
			
			final int MOVIE_FIELDS = 4; //number of fields in array
			String[] movieData = new String[MOVIE_FIELDS]; //array of movie data
			
			String line = fileScanner.nextLine(); //get the next line
			Scanner lineScanner = new Scanner(line); //most recent line scanner
			lineScanner.useDelimiter("/"); //using / as the delimiter
			
			int index = 0; //counter for while loop
			while(lineScanner.hasNext()) { //fill array with movie field data
				//try saving movie fields to array
				try {
					movieData[index] = lineScanner.next();
				}
				//if there are more than 4 fields in file
				catch (ArrayIndexOutOfBoundsException exc){
					System.out.println("Movie File: found entry with " +
							"invalid number of fields.");
					System.exit(0);
				}
				index++;
			}
			
			//if there were fewer than 4 fields exit
			if(index < 3) {
				System.out.println("Movie with fewer than 4 fields found");
				System.exit(0);
 			}
			
			lineScanner.close();
			Movie newMovie = new Movie(movieData); //create movie obj
			addMovie(newMovie); //add the movie to the database
		}
		System.out.println("\nMovies Stored..");
	}
	
	/*
	 * Method: storeShowings
	 * Purpose: parse the showing data from input file and save in db
	 * Parameters:
	 * 		Scanner fileScanner:	scanner of showing input file
	 * Returns: void
	 */
	
	public void storeShowings(Scanner fileScanner) {
		while(fileScanner.hasNextLine()) {	//while there are more lines
			
			final int SHOWING_FIELDS = 3; //number of fields in array
			String[] showData = new String[SHOWING_FIELDS]; //array of show data
			
			String line = fileScanner.nextLine(); //get the next line
			Scanner lineScanner = new Scanner(line); //most recent line scanner
			lineScanner.useDelimiter("/"); //using / as the delimiter
			
			int index = 0; //counter for while loop
			while(lineScanner.hasNext()) { //fill array with showing field data
				//try saving showing fields to array
				try {
					showData[index] = lineScanner.next();
				}
				//if there are more than 3 fields in file
				catch (ArrayIndexOutOfBoundsException exc){
					System.out.println("Showing File: found entry with " +
							"invalid number of fields.");
					System.exit(0);
				}
				index++;
			}
			
			//if there were fewer than 3 fields exit
			if(index < 2) {
				System.out.println("Showing with fewer than 3 " +
						"fields found");
				System.exit(0);
			}
			
			lineScanner.close(); //close scanner
			Showing newShow = new Showing(showData); //create show obj
			addShow(newShow); //add the show to the database
		}
		System.out.println("Shows Stored..");
	}
	
	
	/*
	 * Method: usrAddMovie
	 * Purpose: allow user to manually add movies to db
	 * Parameters:
	 * 		Scanner console:	scanner of console input
	 * Returns: void
	 */
	
	public void usrAddMovie(Scanner console) {
		final int MOVIE_FIELDS = 4; //number of fields in array
		String[] movieData = new String[MOVIE_FIELDS]; //array of movie data
		
		String line = console.nextLine(); //get the next line
		Scanner lineScanner = new Scanner(line); //most recent line scanner
		lineScanner.useDelimiter("/"); //using / as the delimiter
		
		int index = 0; //counter for while loop
		while(lineScanner.hasNext()) { //fill array with movie field data
			//try saving movie fields to array
			try {
				movieData[index] = lineScanner.next();
			}
			//if there are more than 4 fields in file
			catch (ArrayIndexOutOfBoundsException exc){
				System.out.println("Movie File: found entry with " +
						"invalid number of fields.");
				System.exit(0);
			}
			
			index++;
		}
		
		//if there were fewer than 4 fields exit
		if(index < 3) {
			System.out.println("Movie with fewer than 4 fields found");
			System.exit(0);
			}
		
		lineScanner.close();
		Movie newMovie = new Movie(movieData); //create movie obj
		addMovie(newMovie); //add the movie to the database
	}
	
	
	/*
	 * Method: usrAddShow
	 * Purpose: allow user to manually add a single show
	 * Parameters:
	 * 		Scanner console:	scanner of user input
	 * Returns: void
	 */
	
	public void usrAddShow(Scanner console) {
		final int SHOWING_FIELDS = 3; //fields in array
		String[] showData = new String[SHOWING_FIELDS]; //array of show data
		
		String line = console.nextLine(); //get the next line
		Scanner lineScanner = new Scanner(line); //most recent line scanner
		lineScanner.useDelimiter("/"); //using / as the delimiter
		
		int index = 0; //counter for while loop
		while(lineScanner.hasNext()) { //fill array with showing field data
			//try saving showing fields to array
			try {
				showData[index] = lineScanner.next();
			}
			//if there are more than 3 fields in file
			catch (ArrayIndexOutOfBoundsException exc){
				System.out.println("Showing File: found entry with " +
						"invalid number of fields.");
				System.exit(0);
			}
			index++;
		}
		
		//if there were fewer than 3 fields exit
		if(index < 2) {
			System.out.println("Showing with fewer than 3 " +
					"fields found");
			System.exit(0);
		}
		
		lineScanner.close(); //close scanner
		Showing newShow = new Showing(showData); //create show obj
		addShow(newShow); //add the show to the database
	}
	
	
	/*
	 * Method: addMovie
	 * Purpose: add movie to linked list
	 * Parameters: Movie newMovie: the movie obj to add
	 * Returns: void
	 */
	
	public void addMovie(Movie newMovie) {
		movieTree.add(newMovie);
	}
	
	
	/*
	 * Method: addShow
	 * Purpose: add showing to linked list
	 * Parameters: Showing newShow: the showing obj to add
	 * Returns: void
	 */
	
	public void addShow(Showing newShow) {
		
		//movie obj at matched ID index
		Movie matched = movieTree.idMatcher(newShow.getID());
		
		if(matched == null) { //no matching movie
			System.out.println("Showing error: ID mismatch.. show skipped!");
		}
		else { 
			//set title if match found
			newShow.setTitle(matched.getTitle());
			
			ShowList showList = matched.getShowList(); //LL of shows
			showList.addShow(newShow);
		}
	}
	
	/*
	 * Method: validateDB
	 * Purpose: Premove empty movies and showings older than a given date
	 * Parameters: 
	 * 		String dateStr: date to validate with
	 * Returns: void
	 */
	
	public void validateDB(String dateStr) {
			movieTree.validate(dateStr);
	}
	
	
	public void removeAll() {
		movieTree.removeAll();
	}
	
	
	/*
	 * Method: printAll
	 * Purpose: Print all movies and shows
	 * Parameters: none
	 * Returns: void
	 */
	
	public void printAll() {
		movieTree.printTree();
	}
	
	
	/*
	 * Method: writeToDisk
	 * Purpose: write the movie tree and showing lists to disk
	 * Parameters: none
	 * Returns: void
	 */
	
	public void writeToDisk(String moviesName, String showingsName) {
		movieTree.writeToDisk(moviesName, showingsName);
	}
}
